﻿using AutoMapDemo.Models;
using AutoMapper;
using Mapster;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace AutoMapDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Person, PersonA>();
                //cfg.CreateMap<PersonA, Person>();
            });
            configuration.AssertConfigurationIsValid();
            var mapper = configuration.CreateMapper();

            var person = new Person()
            {
                Name = "Leo",
                Age = "200",
                Address = "asdhkjahsd",
                BasePerson = new BasePerson()
                {
                    School = "xxx"
                }
            };

            // 手动
            int count = 10000;
            var list = new List<PersonA>();
            var watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < count; i++)
            {
                var personA = new PersonA
                {
                    Name = person.Name,
                    Age = uint.Parse(person.Age),
                    Address = person.Address,
                    BasePerson = new BasePerson
                    {
                        School = person.BasePerson.School
                    }
                };
                list.Add(personA);
            }
            watch.Stop();
            Console.WriteLine("手动转换---");
            Console.WriteLine(watch.Elapsed);

            // automap
            List<PersonA> list1 = new List<PersonA>();
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();
            for (int i = 0; i < count; i++)
            {
                PersonA personA = mapper.Map<PersonA>(person); ;
                list1.Add(personA);
            }
            watch1.Stop();
            Console.WriteLine("使用AutoMapper转换---");
            Console.WriteLine(watch1.Elapsed);

            // hot automap
            var list2 = new List<PersonA>();
            var watch2 = new Stopwatch();
            watch2.Start();
            for (int i = 0; i < count; i++)
            {
                var personA = mapper.Map<PersonA>(person); ;
                list2.Add(personA);
            }
            watch2.Stop();
            Console.WriteLine("hot后AutoMapper转换---");
            Console.WriteLine(watch2.Elapsed);

            // mapster映射
            var watch3 = new Stopwatch();
            watch3.Start();
            for (int i = 0; i < count; i++)
            {
                var personA = person.Adapt<PersonA>();
            }
            watch3.Stop();
            Console.WriteLine("mapster转换---");
            Console.WriteLine(watch3.Elapsed);

            var watch4 = new Stopwatch();
            watch4.Start();
            for (int i = 0; i < count; i++)
            {
                var personA = person.Adapt<PersonA>();
            }
            watch4.Stop();
            Console.WriteLine("hot后mapster转换---");
            Console.WriteLine(watch4.Elapsed);

            Console.ReadKey();
        }
    }
}
